<?php
/**
 * @Project    SectionCategoryArticleListModule
 * @author     Mathias Hortig
 * @package    SectionCategoryArticleListModule
 * @copyright  Copyright (C) 2011-2012 tuts4you.de . All rights reserved.
 * @license   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
*/
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 


jimport('joomla.utilities.date'); 
jimport( 'joomla.application.component.model' );
jimport( 'joomla.html.parameter' );


class mod_sectioncategoryarticlelistmodulehelper
{
	function GetCategories(&$params)
	{

        $document = &JFactory::getDocument();

		$document->addStyleSheet(JURI::root(true).'/modules/mod_sectioncategoryarticlelistmodule/sectioncategoryarticlelistmodule.css');
		$document->addScript(JURI::root(true).'/modules/mod_sectioncategoryarticlelistmodule/sectioncategoryarticlelistmoduletoggle.js');
		
		require_once JPATH_SITE.'/components/com_content/helpers/route.php';

		$catids = array();
                foreach($params->get('catid') as $key => $value)
		{
			if($value != "")
			{
				$catids[] = $value;
			}
		}
		
		$html;
		
		$ordering = $params->get('ordering','created desc');
		$linkcategories = $params->get('linkcategories',1);
		$usetoggle = $params->get('usetoggle',0);
		$defaulttoggle = $params->get('defaulttoggle',0);
		
		foreach (mod_sectioncategoryarticlelistmodulehelper::GetChildCategories(1,1, $catids, $linkcategories, $usetoggle, $defaulttoggle) as $category)
		{
		  $html .= mod_sectioncategoryarticlelistmodulehelper::PopulateCategory($catids, $category, 1, $linkcategories, $usetoggle, $defaulttoggle, $ordering);
		}
		return $html;
	}
	
	function GetChildCategories($parentId)
    {

        $result = array();
        $database =& JFactory::getDBO();
 		$database->setQuery("SELECT c.id , c.title
				     FROM #__categories AS c
				     WHERE c.parent_id = " . $parentId . " and c.published=1 AND c.extension='com_content' 
					ORDER BY c.lft");

        return $database->loadObjectList();
	}


    function GetArticles($id, $ordering)
    {
        $date = new JDate();
        $now = $date->toSQL();
        $database =& JFactory::getDBO();
        $nullDate = $database->getNullDate();
		
        $database->setQuery('SELECT title, id, catid'
            . ' FROM #__content'
            . " WHERE catid = " . $id . " AND access = 1 AND state = 1"
            . " AND ( publish_up = " . $database->Quote( $nullDate ) . " OR publish_up <= " . $database->Quote( $now ) . " )"
            . " AND ( publish_down = " . $database->Quote( $nullDate ) . " OR publish_down >= " . $database->Quote( $now ) . " )"
            . " ORDER BY " . $ordering

        );
        $results = $database->loadObjectList();
	
	    return $results;
    }
	
	function GetLink($linkcategories, $category)
	{
		if($linkcategories==1)
		{
			return '<a class="categorylink" href="' .  JRoute::_(ContentHelperRoute::getCategoryRoute($category->id)) . '">' . $category->title . '</a>';
		}
		else
		{
			return '<span class="categorylink">'.$category->title.'</span>';
		}
	}
	
	function GetToggleLink($usetoggle, $defaulttoggle)
	{
		if($usetoggle == 1)
		{
			$class = 'togglecategorymodulecollapse';
			if($defaulttoggle!=0)
			{
				$class = 'togglecategorymoduleexpand';
			}
			return '<a href="javascript:void(0);" class="' . $class . '" onclick="ToggleModuleMenu(this);"></a>';
		}
		return '';
	}
	
	function GetArticleClass($usetoggle, $defaulttoggle, $curDepth)
	{
		if($usetoggle == 1)
		{
			if($defaulttoggle!=0)
			{
				if($curDepth > 1)
				{
					return 'style="display:none;"';
				}
			}
		}
		return '';
	}

	function PopulateArticles($usetoggle, $defaulttoggle,$category, $curDepth, $ordering)
	{
		$html;
		$html .= '<ul id="categoryarticles' . $category->id . '">';

		foreach(mod_sectioncategoryarticlelistmodulehelper::GetArticles($category->id, $ordering) as $article)
		{
			$link = '<a href="' .  JRoute::_(ContentHelperRoute::getArticleRoute($article->id, $article->catid)) . '">' . $article->title . '</a>';
			$html .= '<li  ' . mod_sectioncategoryarticlelistmodulehelper::GetArticleClass($usetoggle, $defaulttoggle, $curDepth) . ' class="articledepth' . $curDepth . '">' . $link . '</li>';
		}

		$html .= '</ul>';

		if($html != '<ul id="categoryarticles' . $category->id . '"></ul>')
		{
			return $html;
		}
		return '';
	}
	
	function PopulateCategory($catids, $category, $curDepth, $linkcategories, $usetoggle, $defaulttoggle, $ordering)
	{
	    $html;

		if(in_array($category->id,$catids) || sizeof($catids) == 0)
		{
			$html .= '<li  ' . mod_sectioncategoryarticlelistmodulehelper::GetArticleClass($usetoggle,$defaulttoggle, $curDepth, $ordering) . ' class="categoryDepth' . $curDepth . '">' .
				mod_sectioncategoryarticlelistmodulehelper::GetToggleLink($usetoggle, $defaulttoggle) . mod_sectioncategoryarticlelistmodulehelper::GetLink($linkcategories,$category);

			$html .= mod_sectioncategoryarticlelistmodulehelper::PopulateArticles($usetoggle, $defaulttoggle,$category, $curDepth, $ordering);		
        }
		
		foreach(mod_sectioncategoryarticlelistmodulehelper::GetChildCategories($category->id, $curDepth, $catids, $linkcategories, $usetoggle, $defaulttoggle) as $childCategory)
		{
				if(in_array($category->id,$catids) || sizeof($catids) == 0)
				{
					$html .= '<ul>';
					$html .= mod_sectioncategoryarticlelistmodulehelper::PopulateCategory($catids, $childCategory, $curDepth+1, $linkcategories, $usetoggle, $defaulttoggle, $ordering );
					$html .= '</ul>';
				}
				else
				{
					$html .= mod_sectioncategoryarticlelistmodulehelper::PopulateCategory($catids, $childCategory, $curDepth, $linkcategories, $usetoggle, $defaulttoggle, $ordering);
				}
		}
		
		if(in_array($category->id,$catids) || sizeof($catids) == 0)
		{
			$html .= '</li>';
		}
  
        return $html;
  }
}
